package com.flyco.modules.returns.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyco.common.util.DateUtils;
import com.flyco.common.util.IdGenerator16Bit;
import com.flyco.modules.oms.vo.OmsOrderPage;
import com.flyco.modules.pms.entity.PmsProduct;
import com.flyco.modules.pms.service.IPmsProductService;
import com.flyco.modules.returns.entity.OmsSendOut;
import com.flyco.modules.returns.entity.OmsSendOutItem;
import com.flyco.modules.returns.mapper.OmsSendOutMapper;
import com.flyco.modules.returns.service.IOmsSendOutItemService;
import com.flyco.modules.returns.service.IOmsSendOutService;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.service.ISysUserService;
import com.flyco.modules.ums.model.UmsMemberReceiveAddressModel;
import com.flyco.modules.ums.service.IUmsMemberReceiveAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OmsSendOutServiceImpl extends ServiceImpl<OmsSendOutMapper, OmsSendOut> implements IOmsSendOutService {

    @Autowired
    private IStmStoreInfoService stmStoreInfoService;
    @Autowired
    private IUmsMemberReceiveAddress iUmsMemberReceiveAddress;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IPmsProductService pmsProductService;
    @Autowired
    private IOmsSendOutItemService sendOutItemService;


    //往发货单表插入数据
    @Override
    public OmsSendOut addSendOrder(OmsOrderPage omsOrderPage) {

        //往发货单主表插入数据
        OmsSendOut omsOrder = addSendOrderOut(omsOrderPage);

        //添加发货单详情
        addSendOrderOutItem(omsOrderPage, omsOrder);

        return omsOrder;
    }


    public OmsSendOut addSendOrderOut(OmsOrderPage omsOrderPage) {
        OmsSendOut omsOrder = new OmsSendOut();
        BeanUtils.copyProperties(omsOrderPage, omsOrder);
        omsOrder.setId(String.valueOf(IdGenerator16Bit.getInstance().nextId()));
        omsOrder.setPartnerId(0l);
        omsOrder.setMemberId(omsOrder.getStoreId());
        omsOrder.setOrderSn(omsOrder.getId());
        omsOrder.setCustomerType(5);
        StmStoreInfo stmStoreInfo = stmStoreInfoService.getByStoreId(omsOrder.getStoreId());
        UmsMemberReceiveAddressModel umsMemberReceiveAddressModel = iUmsMemberReceiveAddress.getById(omsOrder.getCompanyAddressId());
        omsOrder.setCompanyName(stmStoreInfo.getName());
        omsOrder.setCompanyCode(stmStoreInfo.getId());
        omsOrder.setMemberUsername(stmStoreInfo.getLinkName());
        omsOrder.setTotalAmount(BigDecimal.ZERO);
        omsOrder.setPayAmount(BigDecimal.ZERO);
        omsOrder.setEffectiveAmount(BigDecimal.ZERO);
        omsOrder.setPayType("icbcToBPay");
        omsOrder.setSourceType("mini");
        omsOrder.setSourceSystem("online");
        omsOrder.setAddressId(umsMemberReceiveAddressModel.getId());
        omsOrder.setReceiverPhone(stmStoreInfo.getPhone());
        omsOrder.setReceiverName(stmStoreInfo.getLinkName());
        omsOrder.setReceiverProvince(umsMemberReceiveAddressModel.getProvince());
        omsOrder.setReceiverProvinceCode(umsMemberReceiveAddressModel.getProvinceCode());
        omsOrder.setReceiverCity(umsMemberReceiveAddressModel.getCity());
        omsOrder.setReceiverCityCode(umsMemberReceiveAddressModel.getCityCode());
        omsOrder.setReceiverRegion(umsMemberReceiveAddressModel.getRegion());
        omsOrder.setReceiverRegionCode(umsMemberReceiveAddressModel.getAreaCode());
        omsOrder.setTown(umsMemberReceiveAddressModel.getTown());
        omsOrder.setTownCode(umsMemberReceiveAddressModel.getTownCode());
        omsOrder.setDistance(umsMemberReceiveAddressModel.getDistance());
        omsOrder.setReceiverDetailAddress(umsMemberReceiveAddressModel.getDetailAddress());
        omsOrder.setStatus(0);
        omsOrder.setOrderType(2);
        omsOrder.setProductStatus(1);
        omsOrder.setPayStatus(1);
        omsOrder.setCustomerCode(stmStoreInfo.getId());

        //获取销售
        SysUser saleUser = userService.getById(stmStoreInfo.getSaleUserId());
        if (saleUser != null) {
            omsOrder.setSaleUserId(Long.valueOf(saleUser.getId()));
            omsOrder.setSaleUserName(saleUser.getRealname());
            omsOrder.setSaleJdCode(saleUser.getJdCode());
        }
        this.save(omsOrder);
        return omsOrder;
    }

    public void addSendOrderOutItem(OmsOrderPage omsOrderPage, OmsSendOut omsOrder) {
        List<OmsSendOutItem> sendOutItemList = new ArrayList<>();
        omsOrderPage.getOmsOrderItemList().forEach(item -> {
            OmsSendOutItem entity = new OmsSendOutItem();
            BeanUtils.copyProperties(item, entity);

            PmsProduct pmsProduct = pmsProductService.getById(entity.getProductId());
            entity.setId(IdGenerator16Bit.getInstance().nextId());
            entity.setProductId(pmsProduct.getId());
            entity.setProductBrand(pmsProduct.getBrandName());
            entity.setProductName(pmsProduct.getName());
            entity.setProductPrice(BigDecimal.ZERO);
            entity.setProductSkuCode(pmsProduct.getSkuCode());
            entity.setProductSkuUnit(pmsProduct.getSkuUnit());
            entity.setProductUnit(pmsProduct.getUnit());
            entity.setRealAmount(BigDecimal.ZERO);
            entity.setTotalRealAmount(BigDecimal.ZERO);
            entity.setOrderId(omsOrder.getId());
            entity.setOrderSn(omsOrder.getId());
            entity.setProductSkuSpec(Integer.valueOf(pmsProduct.getSkuSpec()));
            entity.setProductPic(pmsProduct.getPic());
            entity.setMinUnitQuantity(entity.getProductQuantity() * entity.getProductSkuSpec());
            sendOutItemList.add(entity);
        });
        sendOutItemService.saveBatch(sendOutItemList);
    }


}
